package com.example.screenappreceive


import android.media.projection.MediaProjection
import android.util.Log
import android.view.Surface
import org.java_websocket.WebSocket
import org.java_websocket.client.WebSocketClient
import org.java_websocket.handshake.ClientHandshake
import org.java_websocket.handshake.ServerHandshake
import org.java_websocket.server.WebSocketServer
import java.lang.Exception
import java.net.Socket
import java.net.URI
import java.nio.ByteBuffer
import java.util.*

class SocketLive(var surface: Surface)  :WebSocketClient(URI("ws://192.168.50.106:9088")){

     var  socket:WebSocket ?=null;
     var  h264Player: H264Player

     init {
         h264Player = H264Player(surface)
     }

    fun start() {
        // 链接
        this.connect()
    }
    override fun onOpen(handshakedata: ServerHandshake?) {
        Log.e("启动webServcieClient", handshakedata.toString())
    }

    override fun onMessage(message: String?) {
        Log.e("启动webServcieClient", message.toString())
    }

    override fun onMessage(bytes: ByteBuffer?) {
        super.onMessage(bytes)
        Log.e("不断收到消息", bytes.toString())
        var  buf = ByteArray(bytes!!.remaining())
        bytes?.get(buf)

        h264Player.play(buf)
    }
    override fun onClose(code: Int, reason: String?, remote: Boolean) {
    }

    override fun onError(ex: Exception?) {
        Log.e("onError", ex.toString())
    }


}